package club.zhilin.guli.service.edu.controller.backend;


import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.common.base.result.CommonResultCodeEnum;
import club.zhilin.guli.service.base.exception.GlobalException;
import club.zhilin.guli.service.edu.domain.vo.SubjectVO;
import club.zhilin.guli.service.edu.service.ISubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author zhilin
 * @since 2022-01-11 15:29:01
 */
@Api(tags = "课程分类管理")
@RestController
@RequestMapping("/admin/edu/subject")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SubjectController {

    private final ISubjectService iSubjectService;

    @ApiOperation("Excel批量导入课程分类")
    @PostMapping("/import")
    public CommonResult<String> batchImport(@ApiParam("Excel文件") @RequestParam("file") MultipartFile file) {
        try {
            iSubjectService.batchImport(file.getInputStream());
            return CommonResult.success("批量导入成功");
        } catch (IOException e) {
            throw new GlobalException(CommonResultCodeEnum.EXCEL_DATA_IMPORT_ERROR);
        }
    }

    @ApiOperation("嵌套数据列表")
    @GetMapping("/nestedList")
    public CommonResult<List<SubjectVO>> nestedList() {
        List<SubjectVO> vos = iSubjectService.nestedList("0");
        return CommonResult.success(vos);
    }
}

